package org.zzh.config;

import org.springframework.context.annotation.*;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 张智豪
 * @description SpringMVC配置类
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.zzh") // 扫描控制器
@Import({MybatisConfig.class, ShiroConfig.class}) // 导入 MybatisConfig
@EnableAspectJAutoProxy
public class SpringMVCConfig implements WebMvcConfigurer {
    /**
     * 配置并返回一个InternalResourceViewResolver实例
     * 该实例用于解析视图，将逻辑视图名映射到实际的JSP文件
     *
     * @return InternalResourceViewResolver的实例，用于视图解析
     */
    @Bean
    public InternalResourceViewResolver viewResolver() {
        // 创建一个InternalResourceViewResolver对象
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        // 设置视图解析的前缀为空字符串，意味着不添加任何前缀
        resolver.setPrefix("");
        // 设置视图解析的后缀为".jsp"，意味着逻辑视图名将直接映射到.jsp文件
        resolver.setSuffix(".jsp");
        // 返回配置好的InternalResourceViewResolver对象
        return resolver;
    }

    /**
     * 配置资源处理器以处理静态资源请求
     *
     * @param registry 资源处理器注册表，用于注册静态资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置 CSS 资源路径
        registry.addResourceHandler("/css/**")
                .addResourceLocations("/css/");
        // 配置 img 资源路径
        registry.addResourceHandler("/img/**")
                .addResourceLocations("/img/");
        // 配置 make 资源路径
        registry.addResourceHandler("/make/**")
                .addResourceLocations("/make/");
        //配置第三方资源路径
        registry.addResourceHandler("/plugins/**")
                .addResourceLocations("/plugins/");
    }

    /**
     * 配置文件上传解析器
     *
     * @return CommonsMultipartResolver实例
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        // 创建一个CommonsMultipartResolver对象
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        // 设置上传文件的最大大小（以字节为单位)
        // 10MB
        multipartResolver.setMaxUploadSize(10485760);
        // 设置上传文件的默认编码
        multipartResolver.setDefaultEncoding("UTF-8");
        // 返回配置好的CommonsMultipartResolver对象
        return multipartResolver;
    }

    /**
     * 配置全局日期格式化器
     *
     * @param registry 注册表
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatterForFieldType(Date.class, new CustomDateFormatter("yyyy-MM-dd"));
    }


    @Bean
    public ReentrantLock reentrantLock() {
        return new ReentrantLock();
    }

}